<template>
    <div>
   <el-card class="Hys_nav">
        <el-col>
        <el-button type="primary" class="rolesBtn" @click="Btn"
          >添加分类</el-button
        >
      </el-col>
     <zk-table
        :data="tableData"
        :columns="columns"
        highlight-current="flase"
        border
        :selection-type="false"
        show-index
        index-text="#"
        :expand-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok">
          <li
            class="el-icon-success"
            v-if="(cat_deleted = true)"
            style="color: #39ce39"
          ></li>
          <li class="el-icon-error" v-else style="color: #f00"></li>
        </template>
        <template slot="order">
          <el-tag type="success" v-if="(cat_level = 0)">二级</el-tag>
          <el-tag type="danger" v-else-if="(cat_level = 1)">三级</el-tag>
          <el-tag v-else>一级</el-tag>
        </template>
        <template slot="del" >
          <el-button
            size="mini"
            type="primary"
            >编辑</el-button
          >
          <el-button type="mini">删除</el-button>
        </template>
      </zk-table>

            <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="50%"
                :append-to-body=true
                    >
                <el-form :model="ruleForm" :rules="rules" ref="ref" label-width="100px" class="demo-ruleForm">
                      <el-form-item label="父类名称" prop="cat_name">
                        <el-input v-model="ruleForm.cat_name"></el-input>
                      </el-form-item>
                       <el-form-item label="分类名称" prop="cat_level">
                        <el-input v-model="ruleForm.cat_level"></el-input>
                      </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="BtnQuxiao">取 消</el-button>
                  <el-button type="primary" @click="Btns">确 定</el-button>
                </span>
              </el-dialog>
             <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="queryinfor.pagenum"
                  :page-sizes="[1,3,5,10]"
                  :page-size="queryinfor.pagesize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
                </el-pagination>
   </el-card>
    </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      queryinfor: {
        type: [1, 2, 3],
        pagenum: 1,
        pagesize: 18
      },
      ruleForm: {
        cat_pid: 2,
        cat_name: "",
        cat_level: ""
      },
      dialogVisible: false,
      rules: {
        cat_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      },
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          prop: "cat_deleted",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          prop: "cat_deleted",
          type: "template",
          template: "del"
        }
      ]
    };
  },
  created() {
    this.addForm();
  },
  methods: {
    async addForm() {
      const res = await this.$http.get();
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.tableData = data;
        this.total = data.total;
        console.log(this.tableData);
      }
    },
    Btn() {
      this.dialogVisible = true;
    },
    async Btns() {
      const res = await this.$apis.BtnID(this.ruleForm);
      console.log(res);
      let { meta } = res.data;
      if (meta.status == 201) {
        this.$message.success("创建成功");
        this.dialogVisible = false;
        this.addForm();
      }
    },
    BtnQuxiao() {
      this.dialogVisible = false;
      this.$refs.ref.resetFields();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryinfor.pagesize = val;
      this.addForm();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryinfor.pagenum = val;
      this.addForm();
    }
  }
};
</script>
<style lang="scss">
.Hys_nav {
  margin-top: 10px;
}
.Hys_btn {
  margin-bottom: 10px;
}
</style>
